package corewarsxxi.gui;

import corewarsxxi.logic.Core;
import corewarsxxi.logic.MemoryCell;
import corewarsxxi.logic.Warrior;
import java.awt.Color;
import java.awt.Font;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import corewarsxxi.logic.IObserver;
import java.awt.Dimension;
import javax.swing.border.LineBorder;

/**
 *
 * @author ei10139
 */
public class DebuggerListFrame extends JPanel implements IObserver
{
    private Core c = Core.getInstance();
    private Font fActive = new Font("Courier New", Font.PLAIN, 11);
    private Font fDefault = new Font("Courier New", Font.PLAIN, 11);

    public int memOffset = 0;
    //public int memOffsetActive = 0;
    private JLabel[][] labels;

    public DebuggerListFrame()
    {
        super();
        this.setBackground(Color.white);
        this.setBorder(new LineBorder(Color.lightGray, 1, true));
        this.setPreferredSize(new Dimension(185, 250));

        GroupLayout layout = new GroupLayout(DebuggerListFrame.this);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        ParallelGroup groupHLabels = layout.createParallelGroup();
        SequentialGroup groupVLabels = layout.createSequentialGroup();

        labels = new JLabel[50][4];
        for(int i = 0; i < 30; ++i)
        {
            for(int j = 0; j < 4; ++j)
            {
                labels[i][j] = new JLabel("", SwingConstants.LEFT);
                labels[i][j].setFont(fActive);
                labels[i][j].setForeground(Color.black);
            }
            labels[i][0].setText("" + i);
            labels[i][1].setText("dat");
            labels[i][2].setText("$0");
            labels[i][3].setText("$0");
            groupHLabels.addGroup(
                    layout.createSequentialGroup()
                        .addComponent(labels[i][0])
                        .addComponent(labels[i][1])
                        .addComponent(labels[i][2])
                        .addComponent(labels[i][3])
                    );
            groupVLabels.addGroup(
                    layout.createParallelGroup()
                        .addComponent(labels[i][0])
                        .addComponent(labels[i][1])
                        .addComponent(labels[i][2])
                        .addComponent(labels[i][3])
                    );
        }
        /*
        labels[memOffsetActive][0].setForeground(Color.blue);
        labels[memOffsetActive][1].setForeground(Color.blue);
        labels[memOffsetActive][2].setForeground(Color.blue);
        labels[memOffsetActive][3].setForeground(Color.blue);*/

        layout.setHorizontalGroup(groupHLabels);
        layout.setVerticalGroup(groupVLabels);


        DebuggerListFrame.this.setLayout(layout);
    }

    public void notifyObs(MemoryCell obs, int offset, Warrior currentWarrior)
    {
        if(offset < memOffset || memOffset + 29 < offset)
            return;

        labels[offset - memOffset][1].setText(obs.getOpCode().toString());
        labels[offset - memOffset][2].setText(obs.getaField().toString());
        labels[offset - memOffset][3].setText(obs.getbField().toString());
        for(JLabel l : labels[offset - memOffset])
            l.setForeground(currentWarrior.getColor());
    }

    void setOffset(int offset) {
        this.memOffset = offset;

        for(int i = 0; i < 30; ++i)
        {
            labels[i][0].setText("" + (offset + i));
            labels[i][1].setText("" + this.c.memArray.get(offset + i).getOpCode());
            labels[i][2].setText("" + this.c.memArray.get(offset + i).getaField());
            labels[i][3].setText("" + this.c.memArray.get(offset + i).getbField());

            for(JLabel l : labels[i])
                l.setForeground(Color.black);
        }

        this.repaint();
    }
}
